package com.pucong.gitee.database.backup.business.sys.controller;

import com.pucong.gitee.database.backup.business.sys.service.SysRoleService;
import com.pucong.gitee.database.backup.business.sys.vo.AddSysRoleResourceVo;
import com.pucong.gitee.database.backup.common.entity.sys.SysRole;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@RestController
@RequestMapping("/sys/roleInfo")
@Api(value = "用户基础信息-API ",tags = "02. 用户管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/getSysRoleInfoList")
    @ApiOperation(value = "01条件查询")
    public Res<List<SysRole>> getSysRoleInfoList(SysRole sysRole){
        List<SysRole> sysRoleInfoList = sysRoleService.getSysRoleInfoList(sysRole);
        return Res.ok(sysRoleInfoList);
    }

    @PostMapping("/insertSysRoleInfo")
    @ApiOperation(value = "02新增")
    public Res<Integer> insertSysRoleInfo(@Valid @RequestBody SysRole sysRole){
        return Res.ok(sysRoleService.insertSysRoleInfo(sysRole));
    }

    @PutMapping("/updateSysRoleInfoById/{id}")
    @ApiOperation(value = "03根据用户id更新数据")
    public Res<Integer> updateSysRoleInfoById(@ApiParam("主键")@PathVariable("id") String id,@RequestBody SysRole sysRole){
        return Res.ok(sysRoleService.updateSysRoleInfoById(id, sysRole));
    }

    @DeleteMapping("/deleteSysRoleInfoById/{id}")
    @ApiOperation(value = "04根据主键id删除数据")
    public Res<Integer> deleteSysRoleInfoById(@ApiParam("主键")@PathVariable("id") String id){
        return Res.ok(sysRoleService.deleteSysRoleInfoById(id));
    }

    @PostMapping("/addSysRoleResource/{id}")
    @ApiOperation(value = "05新增角色关联资源")
    public Res<Integer> addSysRoleResource(@ApiParam("角色主键")@PathVariable("id") String id, @Valid @RequestBody AddSysRoleResourceVo addSysRoleResourceVo){
        return Res.ok(sysRoleService.addSysRoleResource(id,addSysRoleResourceVo));
    }

    @PostMapping("/deleteSysRoleResource/{id}")
    @ApiOperation(value = "06删除角色关联资源")
    public Res<Integer> deleteSysRoleResource(@ApiParam("关联主键")@PathVariable("id") String id){
        return Res.ok(sysRoleService.deleteSysRoleResource(id));
    }

    @PostMapping("/addSysRoleUser/{id}/{userId}")
    @ApiOperation(value = "07新增角色关联用户")
    public Res<Integer> addSysRoleUser(@ApiParam("角色主键")@PathVariable("id") String id,@ApiParam("用户ID")@PathVariable("userId") String userId){
        return Res.ok(sysRoleService.addSysRoleUser(id, userId));
    }

    @PostMapping("/deleteSysRoleUser/{id}")
    @ApiOperation(value = "08删除角色关联用户")
    public Res<Integer> deleteSysRoleUser(@ApiParam("关联主键")@PathVariable("id") String id){
        return Res.ok(sysRoleService.deleteSysRoleUser(id));
    }
}
